package com.gk.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.pojo.CouponHistory;
import com.eight.pojo.TbCoupon;
import com.eight.util.TimeUtil;
import com.eight.vo.ComResult;
import com.gk.dao.CouponHistoryDao;
import com.gk.fein.UserEnableCouponFein;
import com.gk.service.CouponHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * @author: GK
 * @date:  2023/3/2 11:34
 * @version: No1.0
 */
@Service
public class CouponHistoryServiceImpl  extends ServiceImpl<CouponHistoryDao,CouponHistory> implements CouponHistoryService {
    @Resource
    private CouponHistoryDao couponHistoryDao;

    @Autowired
    private UserEnableCouponFein userEnableCouponFein;

    /**
     * 条件分页查询
     * @param couponHistory 条件
     * @param currentPage 当前页
     * @param pageSize 页中数据
     * @return 响应前端
     */
    @Override
    public ComResult selectAll(CouponHistory couponHistory, Integer currentPage, Integer pageSize) {
        Page<CouponHistory> page = new Page<>(currentPage, pageSize);
        page=couponHistoryDao.selectAll(page,couponHistory);
        if(page!=null){
            return new ComResult(2000,"查询成功",page);
        }else{
            return new ComResult(5000,"查询失败");
        }
    }

    /**
     * 根据条件删除
     * @param id 条件
     * @return 响应前端数据
     */
    @Override
    public ComResult deleteId(Integer id) {
        int i = couponHistoryDao.deleteById(id);
        if(i>0){
            return new ComResult(2000,"删除成功");
        }else{
            return  new ComResult(5000,"删除失败");
        }
    }

    /**
     * 根据用户名字以及优惠券id插入一条记录
     *
     * @param username 用户的名字
     * @param cid      优惠券的id
     * @return 插入的结果
     */
    @Override
    public int insertIntegrationHistory(String username, Integer cid, Integer type) {
        // 费恩查询优惠券尸体信息
        TbCoupon coupon = userEnableCouponFein.getCouponByCid(cid);
        // 制作优惠券信息
        CouponHistory couponHistory = new CouponHistory();
        couponHistory.setCouponId(coupon.getName())
                .setCustomerName(username)
                .setCreateTime(TimeUtil.now(""))
                .setUseStatus(type);
        // 执行dao操作
        return couponHistoryDao.insert(couponHistory);
    }
}
